Previous | Home | Next |
There are two main components in annotations. First is annotation type and the next is the annotation itself which we use in the code to add meaning. Every annotation belongs to a annotation type.
Annotation Type:
@interface { method declaration; }
- Annotation type is very similar to an interface with little difference.
- We attach ‘@’ just before interface keyword.
- Methods will not have parameters.
- Methods will not have throws clause.
- Method return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.
- We can assign a default value to method.
@Target(ElementType.METHOD) public @interface MethodInfo { }
-
Documented When a annotation type is annotated with @Documented then wherever this annotation is used those elements should be documented using Javadoc tool.
-
Inherited This meta annotation denotes that the annotation type can be inherited from super class. When a class is annotated with annotation of type that is annotated with Inherited, then its super class will be queried till a matching annotation is found.
-
Retention This meta annotation denotes the level till which this annotation will be carried. When an annotation type is annotated with meta annotation Retention, RetentionPolicy has three possible values:
@Retention(RetentionPolicy.RUNTIME) public @interface Developer { String value(); }
Class When the annotation value is given as ‘class’ then this annotation will be compiled and included in the class file.
Runtime The value name itself says, when the retention value is ‘Runtime’ this annotation will be available in JVM at runtime. We can write custom code using reflection package and parse the annotation. I have give an example below.
Source This annotation will be removed at compile time and will not be available at compiled class.
-
Target This meta annotation says that this annotation type is applicable for only the element (ElementType) listed. Possible values for ElementType are, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE.
@Target(ElementType.FIELD) public @interface FieldInfo { }
@Documented, @Inherited, @Retention and @Target are the four available meta annotations that are built-in with Java. Apart from these meta annotations we have the following annotations.
@Override When we want to override a method, we can use this annotation to say to the compiler we are overriding an existing method. If the compiler finds that there is no matching method found in super class then generates a warning. This is not mandatory to use @Override when we override a method. But I have seen Eclipse IDE automatically adding this @Override annotation. Though it is not mandatory, it is considered as a best practice.
@Deprecated When we want to inform the compiler that a method is deprecated we can use this. So, when a method is annotated with @Deprecated and that method is found used in some place, then the compiler generates a warning.
@SuppressWarnings This is like saying, “I know what I am doing, so please shut up!” We want the compiler not to raise any warnings and then we use this annotation.
Previous | Home | Next |